/**********
<moduleId>tipCenter</moduleId>
<version>1.0</version>
<date>2018.04.10</date>
**********/
;(function(factory){
    if(typeof define === 'function' && define.amd){
        define(['jquery','toastr'],factory);
    }else{
        factory(jQuery,toastr);
    }
}(function($,toastr){
	  'use strict';
	  if (typeof toastr === 'undefined') {
		  throw new Error('TipCenter requires toastr component')
	  }
	  var DataKey = 'framework.messagecenter';
	  var Default = {
			  closeButton: true,
              debug: false,
              newestOnTop: false,
              closeOnHover:false,
              progressBar: true,
              rtl: false,
              positionClass: 'toast-top-right',
              showDuration: "300",  
              hideDuration: "1000",  
              timeOut: "2000",  
              extendedTimeOut: "1000",  
              showEasing: "swing",  
              hideEasing: "linear",  
              showMethod: "fadeIn",  
              hideMethod: "fadeOut"
	  };
	  
	  var ClassName = {
	    info: 'info',
	    success: 'success',
	    warning: 'warning',
	    error: 'error'
	  };
	  var $lastWaring;

	  // TipCenter Class Definition
	  // =========================
	  var TipCenter = function TipCenter(element, options) {
	    this.element = element;
	    this.options = options;
	    toastr.options = this.options;
	    
	  };

	  TipCenter.prototype.show = function (type,message,title) {
		  return toastr[type](message,title);
	  };
	  TipCenter.prototype.info = function (message,title) {
		  this.show(ClassName.info,message,title || '提示信息:');
	  };
	  TipCenter.prototype.success = function (message,title) {
		  if($lastWaring){
			  toastr.clear($lastWaring, { force: true });
		  }
		  this.show(ClassName.success,message,title || '操作成功:');
	  };
	  TipCenter.prototype.warning = function (validateMessages,title) {
		  
		  
		  var html="";
		  if(validateMessages && (typeof validateMessages === 'string')){
			  html = validateMessages;
		  }else if(validateMessages && (typeof validateMessages === 'object') && (toString.apply(validateMessages) === '[object Array]')){
			  var message = new Object();
			  $.each(validateMessages,function(index,item){
			        
		        if(! message[(item.name) || (item.element)]){
		          message[(item.name) || (item.element)] = new Array();
		        }
		        message[(item.name) || (item.element)].push((item.value || item.message));
		      });
		      $.each(message ,function(key,group){
		        $.each(group,function(tempindex,fieldMessage){
		          if(tempindex == (group.length -1)){
		            html += fieldMessage;
		          }else{
		            html += (fieldMessage+"</br>");
		          } 
		        });
		      });
		  }
		  if(html && !(html=="")){
			  if($lastWaring){
				  toastr.clear($lastWaring, { force: true });
			  }
			  toastr.options.hideDuration=0; 
			  toastr.options.timeOut=0; 
			  $lastWaring = this.show(ClassName.warning,html,title  || '验证结果:');
			  toastr.options.hideDuration=1000; 
			  toastr.options.timeOut=2000; 
		  }
		   
	  };
	  TipCenter.prototype.error = function (message,title) {
		  this.show(ClassName.error,message,title || '错误:');
	  };
	  TipCenter.prototype.clear = function () {
		  toastr.clear();
	  };
	  

	  

	  
	  var old = $.fn.tipCenter;

	  $.fn.tipCenter             = function tipCenter(option,value){
		  var retval;
	        var $set = this.each(function () {
	            var $this = $(this);
	            var data = $this.data(DataKey);
	            var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option);

	            if (!data) $this.data(DataKey, (data = new TipCenter(this, options)));
	            if (typeof option === 'string') retval = data[option](value);
	        });
	        return (retval === undefined) ? $set : retval;
	  };
	  $.fn.tipCenter.Constructor = TipCenter;

	  // No Conflict Mode
	  // ================
	  $.fn.tipCenter.noConflict = function () {
	    $.fn.tipCenter = old;
	    return this;
	  };
	  $('body').tipCenter();
	  

	  

}));